//
//  TextField.swift
//  GreenDeal
//
//  Created by Moennig Q on 8/12/2022.
//

import UIKit
import RxSwift
import RxGesture
import RxCocoa
class BaseTextField: UITextField {
    private lazy var disposeBag = DisposeBag()
    override init(frame: CGRect) {
        super.init(frame: frame)
        //监听字体变化
        Constant.standardViewFlagSuject.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.setRealFontSize(strongSelf.font)
        }.disposed(by: self.disposeBag)
    }
    //MARK: - placeholder和输入文本按照内容长度自动调节字体大小
    func enabledAutoAdjustFontSize(){
        //placeholder和输入文本，按照内容自动调节大小
        self.adjustsFontSizeToFitWidth = true
        self.minimumFontSize = 5
    }
    //MARK: - 正常设置font
    func setFont(_ font: UIFont?) {
        if let font = font {
            if Constant.standardViewFlag {
                self.font = font
            }else{
                self.font = font.withSize(font.pointSize * Constant.magnifiedViewScale)
            }
            
        }
    }
    //MARK: - 设置真正的size
    private func setRealFontSize(_ font: UIFont?){
        if let font = font {
            self.font = font.withSize(font.pointSize * Constant.currentSizeScale)
        }
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class DIYTextField: BaseTextField {
    lazy var disposeBag = DisposeBag()
    static let height: CGFloat = 42
    var leftImageView: UIImageView?
    var rightImageView: UIImageView?
    var requiredLabel = ViewFactory.shared.generateRequiredLabel()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initView()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initView() {
        self.backgroundColor = .white
        self.borderStyle = .none
        self.layer.borderColor = UIColor(hexStr: "#292D3233").cgColor
        self.layer.borderWidth = 1
        self.layer.cornerRadius = Constant.StandardCornerRadius
        self.clipsToBounds = true
        self.textColor = UIColor(hexStr: Constant.TextFieldColorHexStr)
        self.setFont(R.font.interRegular(size: 15))
        self.textAlignment = .left
        self.leftViewMode = .always//left视图一致显示，默认有10的间隔
        self.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
        self.clearButtonMode = .always
        self.tintColor = UIColor(hexStr: Constant.TextFieldColorHexStr)//光标颜色
    }
    func disabled(textHexStr: String, backHexStr: String){
        self.isEnabled = false
        self.textColor = UIColor(hexStr: textHexStr)
        self.backgroundColor =  UIColor(hexStr: backHexStr)
    }
    
    //显示右边的图表按钮,rightView和clear view不能同时显示，leftView不为空则clear view会被覆盖
    func showRightImageView(_ imageName: String, closure: (()->Void)? = nil){
        if let image = UIImage(named: imageName){
            rightImageView = UIImageView(image: image)
            rightImageView?.contentMode = .center
            self.addSubview(rightImageView!)
            rightImageView?.snp.makeConstraints({ make in
                make.right.equalToSuperview()
                make.height.width.equalTo(40)
                make.centerY.equalToSuperview()
            })
            //
            rightImageView!.rx.tapGesture().when(.recognized).subscribe(onNext: {  _  in
                closure?()
            }).disposed(by: self.disposeBag)
        }
    }
    //显示右边的图表按钮
    func showLeftImageView(_ imageName: String){
        if let image = UIImage(named: imageName){
            leftImageView = UIImageView(image: image)
            leftImageView?.contentMode = .center
            leftImageView?.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
            self.leftView = leftImageView!
        }
    }
    
    //MARK: - 控制清除按钮的位置,如果有rightImageView，就调整clear的位置，让rightImageView和clear共存
    override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
        let rect = super.clearButtonRect(forBounds: bounds)
        if self.rightImageView != nil {
            return rect.offsetBy(dx: -40, dy: 0)
        }else{
            return rect.offsetBy(dx: 0, dy: 0)
        }
    }
    //MARK: - 控制leftView的位置
    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
        if let _ =  self.leftView {//有image 图标，编辑框有有10个点的间隔
            return CGRect(x: 0, y: 0, width: self.leftView!.frame.size.width, height: self.leftView!.frame.size.height)
        }
        return super.leftViewRect(forBounds: bounds)
    }
    //MARK: - 文本框 显示 时的 位置 及 显示范围
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        let rect = super.textRect(forBounds: bounds)
        if self.rightImageView != nil {//rightImageView不是设置rightView，所以要手动减去40
            return CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width - 40, height: rect.size.height)
        }else{
            return CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width  , height: rect.size.height)
        }
    }
    //MARK: - 文本框 编辑 时的 位置 及 显示范围
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        let rect = super.textRect(forBounds: bounds)
        if self.rightImageView != nil {//rightImageView不是设置rightView，所以要手动减去40
            return CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width - 40, height: rect.size.height)
        }else{
            return CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width  , height: rect.size.height)
        }
    }
}
